/** AppointmentDao */
package server.service.appointment;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import server.Database;
import server.service.doctor.DoctorModel;
import server.service.patient.PatientModel;
import domain.appointment.Appointment;
import domain.doctor.Doctor;
import domain.patient.Patient;

/**
 * Talks to the database about appointments
 * @author jessienewman
 * @version $Revision: 1.0 $
 */
public class AppointmentDao {
	/**
	 * Field DATABASE.
	 */
	private Database database; 
	
	/**
	 * Field months.
	 */
	private static final int MONTHS = 12;
	
	/**
	 * Constructor for AppointmentDao.
	 */
	public AppointmentDao() {
		database = Database.DATABASE;
	}
	
	/**
	 * Method getDoctorAppointments.
	 * @param doctorId Integer
	
	 * @return ArrayList<Appointment> */
	public List<Appointment> getDoctorAppointments(Integer doctorId) {
		DoctorModel dm = new DoctorModel();
		Doctor d = dm.getDoctor(doctorId);
		List<Appointment> list = new ArrayList<Appointment>();
		for (Appointment a : database.appointments) {
			if (a.getDoctor().equals(d)) {
				list.add(a);
			}
		}
		return list;
	}
	
	/**
	 * Method getPatientAppointments.
	 * @param patientId Integer
	
	 * @return ArrayList<Appointment> */
	public List<Appointment> getPatientAppointments(Integer patientId) {
		PatientModel pm = new PatientModel();
		Patient patient = pm.getPatient(patientId);
		List<Appointment> list = new ArrayList<Appointment>();
		for (Appointment a : database.appointments) {
			if (a.getPatient().equals(patient)) {
				list.add(a);
			}
		}
		return list;
	}
	
	/**
	 * Method getFuturePatientAppointments.
	 * @param patientId Integer
	
	 * @return ArrayList<Appointment> */
	public List<Appointment> getFuturePatientAppointments(Integer patientId) {
		PatientModel pm = new PatientModel();
		Patient patient = pm.getPatient(patientId);
		List<Appointment> list = new ArrayList<Appointment>();
		for (Appointment a : database.appointments) {
			if (a.getPatient().equals(patient) && a.getDate().after(new Date())) {
				list.add(a);
			}
		}
		return list;
	}
	
	/**
	 * Method getFutureDoctorAppointments.
	 * @param doctorId Integer
	
	 * @return ArrayList<Appointment> */
	public List<Appointment> getFutureDoctorAppointments(Integer doctorId) {
		DoctorModel dm = new DoctorModel();
		Doctor d = dm.getDoctor(doctorId);
		List<Appointment> list = new ArrayList<Appointment>();
		for (Appointment a : database.appointments) {
			if (a.getDoctor().equals(d) && a.getDate().after(new Date())) {
				list.add(a);
			}
		}
		return list;
	}
	
	/**
	 * Method getLastMonthDoctorAppointments.
	 * @param doctorId Integer
	
	 * @return ArrayList<Appointment> */
	public List<Appointment> getLastMonthDoctorAppointments(Integer doctorId) {
		Date today = new Date();
		int date = today.getMonth() + MONTHS * today.getYear();
		List<Appointment> allAppointments = getDoctorAppointments(doctorId);
		List<Appointment> lastMonthAppts = new ArrayList<Appointment>();
		for (Appointment a : allAppointments) {
			if (MONTHS * a.getDate().getYear() + a.getDate().getMonth() + 1 == date) {
				lastMonthAppts.add(a);
			}
		}
		return lastMonthAppts;
	}
	
	/**
	 * Method addAppointment.
	 * @param a Appointment
	 */
	public void addAppointment(Appointment a) {
		database.addAppointment(a);
	}
	
	/**
	 * Method removeAppointment.
	 * @param appointment Appointment
	 */
	public void removeAppointment(Appointment appointment) {
		database.appointments.remove(appointment);
	}
	
	/**
	 * super's toString
	
	 * @return super's toString */
	public String toString() {
		return super.toString();
	}
	
	/**
	 * Method save.
	 */
	public void save() {
		database.saveAppointments();
	}
}
